package com.example.prom.qianduoduo.y2025.parser;

import com.example.prom.qianduoduo.y2025.model.MedicineInfo;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MedicineHtmlParser {
    private static final Logger logger = LoggerFactory.getLogger(MedicineHtmlParser.class);

    public static MedicineInfo parse(Document doc) {
        MedicineInfo info = new MedicineInfo();

        // 解析药品名称
        Element titleElement = doc.select("h1.title a.cl-blue").first();
        if (titleElement != null) {
            info.setMedicineName(titleElement.text());

            // 检查title和medicineName是否一致
            if (!titleElement.text().equals(info.getMedicineName())) {
                logger.warn("药品名称不一致: title={}, medicineName={}",
                    titleElement.text(), info.getMedicineName());
            }
        }
        // 解析生产企业
        Element manufacturerElement = doc.select("div.content:contains(【生产企业】)").first();
        if (manufacturerElement != null) {
            info.setProductionEnterprise(manufacturerElement.select("a.cl-blue").text());
        }
        // 解析修改日期
        Element modifyDateElement = doc.select("div.content.hbg:contains(修改日期)").first();
        Element modifyDateElement2 = doc.select("div.content:contains(修改日期)").first();
        if (modifyDateElement != null) {
            info.setModificationDate(modifyDateElement.text().replace("修改日期：", "").trim());
        } if (modifyDateElement2 != null){
            info.setModificationDate(modifyDateElement2.text().replace("修改日期：", "").trim());
        }
        // 解析通用名称
        Element genericNameElement = doc.select("div.content.hbg:contains(通用名称)").first();
        if (genericNameElement != null) {
            info.setMedicineName(genericNameElement.select("a.cl-blue").text());
        }
        // 解析成分
        Element compositionElement = doc.select("div.content:contains(【成分】)").first();
        if (compositionElement != null) {
            info.setComponents(compositionElement.text().replace("【成分】", "").trim());
        }
        // 解析性状
        Element propertiesElement = doc.select("div.content:contains(【性状】)").first();
        if (propertiesElement != null) {
            info.setProperties(propertiesElement.text().replace("【性状】", "").trim());
        }
        // 解析适应症/功能主治
        Element indicationsElement = doc.select("div.content:contains(【适应症】), div.content:contains(【功能主治】)").first();
        if (indicationsElement != null) {
            info.setIndications(indicationsElement.text()
                    .replace("【适应症】", "")
                    .replace("【功能主治】", "")
                    .trim());
        }
        // 解析规格
        Element specificationsElement = doc.select("div.content:contains(【规格】)").first();
        if (specificationsElement != null) {
            info.setSpecifications(specificationsElement.text().replace("【规格】", "").trim());
        }
        // 解析用法用量
        Element usageDosageElement = doc.select("div.content:contains(【用法用量】)").first();
        if (usageDosageElement != null) {
            info.setUsageDosage(usageDosageElement.text().replace("【用法用量】", "").trim());
        }
        // 解析不良反应
        Element adverseReactionsElement = doc.select("div.content:contains(【不良反应】)").first();
        if (adverseReactionsElement != null) {
            info.setAdverseReactions(adverseReactionsElement.text().replace("【不良反应】", "").trim());
        }
        // 解析禁忌
        Element contraindicationsElement = doc.select("div.content:contains(【禁忌】)").first();
        if (contraindicationsElement != null) {
            info.setContraindications(contraindicationsElement.text().replace("【禁忌】", "").trim());
        }
        // 解析注意事项
        Element precautionsElement = doc.select("div.content:contains(【注意事项】)").first();
        if (precautionsElement != null) {
            info.setPrecautions(precautionsElement.text().replace("【注意事项】", "").trim());
        }
        // 解析药物相互作用
        Element drugInteractionsElement = doc.select("div.content:contains(【药物相互作用】)").first();
        if (drugInteractionsElement != null) {
            info.setDrugInteractions(drugInteractionsElement.text().replace("【药物相互作用】", "").trim());
        }
        // 解析临床试验
        Element clinicalTrialsElement = doc.select("div.content:contains(【临床试验】), div.content:contains(【临床实验】)").first();
        if (clinicalTrialsElement != null) {
            info.setClinicalTrials(clinicalTrialsElement.text()
                    .replace("【临床试验】", "")
                    .replace("【临床实验】", "")
                    .trim());
        }
        // 解析药理毒理
        Element pharmacologyToxicologyElement = doc.select("div.content:contains(【药理毒理】)").first();
        if (pharmacologyToxicologyElement != null) {
            info.setPharmacologyToxicology(pharmacologyToxicologyElement.text().replace("【药理毒理】", "").trim());
        }
        // 解析贮藏
        Element storageElement = doc.select("div.content:contains(【贮藏】)").first();
        if (storageElement != null) {
            info.setStorage(storageElement.text().replace("【贮藏】", "").trim());
        }
        // 解析包装
        Element packagingElement = doc.select("div.content:contains(【包装】)").first();
        if (packagingElement != null) {
            info.setPackaging(packagingElement.text().replace("【包装】", "").trim());
        }
        return info;

    }
}

